<template>
  <div id="app">
    <h1>在线银行修改密码</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="卡号" prop="cardNo">
        <el-input v-model.number="ruleForm.cardNo" disabled></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updatePass } from "../../api/user";

export default {
  name: "update",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        cardNo: localStorage.getItem("cardNo"),
        pass: "",
        checkPass: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(ruleForm) {
      console.log(ruleForm);
      this.$refs[ruleForm].validate(async (valid) => {
        if (valid) {
          console.log(ruleForm);
          let result = await updatePass(
            this.ruleForm.cardNo,
            this.ruleForm.pass,
            this.ruleForm.checkPass
          );
          this.$message({
            showClose: true,
            message:
              "恭喜你，这是修改成功密码,你需要重写登录账号(2秒后自动回到登录页)",
            type: "success",
            duration: 2000,
          });
          setTimeout(() => {
            // 方法区
            this.$router.push("/");
          }, 2000);

          localStorage.removeItem("cardNo");
        } else {
          this.$message({
            showClose: true,
            message: "请输入卡号错误",
            type: "error",
          });
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.demo-ruleForm {
  margin: auto;
  width: 500px;
}
</style>